<!DOCTYPE html>
<title>Test Context Menu Key, Shift+F10 behavior when preventing default</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<a href="#" id="anchor">Anchor</a>
<script>
test(function() {
    assert_not_equals(window.eventSender, undefined, 'This test requires eventSender.');

    var contextMenuFired = false;
    var preventKeydown = false;
    var preventKeyup = false;
    var anchor = document.getElementById('anchor');
    anchor.addEventListener('contextmenu', () => contextMenuFired = true);
    anchor.addEventListener('keydown', event => preventKeydown ? event.preventDefault() : true);
    anchor.addEventListener('keyup', event => preventKeyup ? event.preventDefault() : true);

    function testContextMenuEvent(key, modifiers, shouldFire, shouldPreventKeydown, shouldPreventKeyup) {
        contextMenuFired = false;
        preventKeydown = shouldPreventKeydown;
        preventKeyup = shouldPreventKeyup;
        anchor.focus();
        eventSender.keyDown(key, modifiers);
        // Esc key to hide context menu
        eventSender.keyDown("Escape");
        assert_equals(contextMenuFired, shouldFire, `${key}+${modifiers} opens Context Menu:${shouldFire} PreventKeydown:${shouldPreventKeydown} PreventKeyup:${shouldPreventKeyup}.`);
    }

    // Shift+F10 should always bind to keydown.
    testContextMenuEvent('F10', ['shiftKey'], true, false, false);
    testContextMenuEvent('F10', ['shiftKey'], false, true, false);
    testContextMenuEvent('F10', ['shiftKey'], true, false, true);

    // ContextMenu key should bind to keyup on Windows and keydown on other platforms.
    testContextMenuEvent('ContextMenu', [], true, false, false);
    const isWin = navigator.platform.indexOf('Win') == 0;
    if (isWin) {
        testContextMenuEvent('ContextMenu', [], true, true, false);
        testContextMenuEvent('ContextMenu', [], false, false, true);
    } else {
        testContextMenuEvent('ContextMenu', [], false, true, false);
        testContextMenuEvent('ContextMenu', [], true, false, true);
    }
}, 'Shift+F10 should bind to keydown, ContextMenu key should bind to keyup only on Windows.');
</script>
